package com.yx.cloud.animator;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import com.yx.cloud.R;
import com.yx.cloud.utils.AppUtil;
import com.yx.cloud.utils.RomUtils;

public class RestrictAreaSetHelpDialog extends Dialog {
    private Context context;
    private ConfirmCallback confirmCallback;
    private int viewId = -1;
    private RestrictAreaClickView restrictAreaClickView;

    public RestrictAreaSetHelpDialog(Context context,int viewId, ConfirmCallback confirmCallback) {
        super(context, R.style.dialog);
        this.context = context;
        this.confirmCallback = confirmCallback;
        this.viewId = viewId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(viewId);

        LinearLayout btn_confirm = findViewById(R.id.ll_I_know);
        restrictAreaClickView = findViewById(R.id.click_view);
        restrictAreaClickView.ivSave = findViewById(R.id.iv_help_save);
        restrictAreaClickView.llSelectItem = findViewById(R.id.ll_select_item_help);
        restrictAreaClickView.llNogo = findViewById(R.id.ll_nogo_help);
        restrictAreaClickView.llNomop = findViewById(R.id.ll_nomop_help);
        restrictAreaClickView.rlArea = findViewById(R.id.rl_restrict_area_help);
        restrictAreaClickView.rlLine = findViewById(R.id.rl_restrict_line_help);
        restrictAreaClickView.rlModeSelect = findViewById(R.id.rl_restrict_mode_select_help);
        restrictAreaClickView.tvRestrict = findViewById(R.id.tv_restrict_mode_help);
        restrictAreaClickView.ivArea = findViewById(R.id.iv_restrict_area_help);
        restrictAreaClickView.ivLine = findViewById(R.id.iv_restrict_line_help);
        restrictAreaClickView.tvLine = findViewById(R.id.tv_restrict_line_help);
        restrictAreaClickView.ivSelect = findViewById(R.id.iv_select_help);
        restrictAreaClickView.llBottom = findViewById(R.id.rl_restricted_area_bottom_help);

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (confirmCallback != null) {
                    //如果confirm返回false, 不取消对话框
                    if(confirmCallback.onConfirm()){
                        dismiss();
                    }
                } else {
                    dismiss();
                }
                restrictAreaClickView.endAnimator = true;
            }
        });

        setCanceledOnTouchOutside(false);

        if(AppUtil.getPackageName(this.context).equals("com.yx.b34")){
            restrictAreaClickView.viewType = RestrictAreaClickView.VIEW_TYPE_YINGXING;
        }else {
            restrictAreaClickView.viewType = RestrictAreaClickView.VIEW_TYPE_INXNI;
        }
    }

    public interface ConfirmCallback {
        boolean onConfirm();

        boolean onCancel();
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private Context context;
        private String message;
        private ConfirmCallback confirmCallback;
        private int viewId = -1;

        private Builder() {
        }

        public Builder context(Context context) {
            this.context = context;
            return this;
        }

        public Builder viewId(int viewId) {
            this.viewId = viewId;
            return this;
        }

        public Builder confirmCallback(ConfirmCallback confirmCallback) {
            this.confirmCallback = confirmCallback;
            return this;
        }

        public RestrictAreaSetHelpDialog build() {
            if (context == null) {
                throw new IllegalArgumentException("context should not be null");
            }
            RestrictAreaSetHelpDialog restrictAreaSetHelpDialog = new RestrictAreaSetHelpDialog(context,viewId,confirmCallback);
            return restrictAreaSetHelpDialog;
        }
    }
}
